#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main (int argc, char const* argv[])
{
    int n, len, i, h, m, ans;
    char buf[8];

    scanf("%d", &n);
    while (n--) {
        scanf("%s", buf);
        len = strlen(buf);

        for (i = 0; i < len; i++) {
            if (buf[i] == ':') {
                buf[i] = '\0';
                h = atoi(buf);
                m = atoi(buf + i + 1);
                break;
            }
        }

        if (m > 0) {
            ans = 0;
        } else {
            if (h > 12) {
                ans = h - 12;
            } else {
                ans = h + 12;
            }
        }

        printf("%d\n", ans);
    }

    return 0;
}
